package cpr.command;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import cpr.DefaultCommandHandler;
import cpr.Printer;
import cpr.Rule;
import cpr.context.Context;
import cpr.context.RenumberContext;
import cpr.data.HLVector;
import cpr.helper.ScoreHelper;
import data.Dictionary;

public class CmdRenumber extends DefaultCommandHandler {
	public CmdRenumber() {
		super("renumber");
		setQuickInfo("digitalize raw data. all strings values get numbers");
		setUsage("no params. [SCORE] (i.e. 0,3,0,0,0 for renumbering just given single score");
		setStartInSeparateThread(false);
	}

	public void execute(String[] params) {
		ScoreHelper helper = getCommandContext().getContext().getScoreHelper();
		int score = helper.convertStringToScore(getParameter(params, 1, "0"));
		RenumberContext renumberContext = getCommandContext().getContext().getRenumberContext();
		renumberContext.TEST_SCORE = score;
		hl_renumber(renumberContext);
	}

	private void doit(HLVector vector, RenumberContext renumberContext) {
//		Rule.totalRuleNr = 0;
		System.gc();
        renumberContext.selectDistinct(false, vector);
		int []hlis = renumberContext.getContext().getScoreHelper().firstSetScoreOf(vector);
		int hl=hlis[0];
		int hli=hlis[1];
		 
		Printer.println(Printer.currDate() + " " + hl + "/" + hli
				+ " free:" + Runtime.getRuntime().freeMemory() + "/"
				+ Runtime.getRuntime().maxMemory());	
	}
	
	public void hl_renumber(RenumberContext renumberContext) {
		Printer.println("renumbering...");
		Context ctx = getCommandContext().getContext();
		
		renumberContext.dictFile = ctx.getDictDataFilePath() ;

		try {
			File df = new File(renumberContext.dictFile);
			if (df.exists()) {
				df.delete();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
        
        Map dictionaryPresets = new HashMap();
        dictionaryPresets.put(Integer.valueOf(0), "*");
        try {
            renumberContext.writeToDictionary(dictionaryPresets.keySet(), dictionaryPresets);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        renumberContext.counter = dictionaryPresets.keySet().size();
        
		HLVector nullvector = new HLVector(new int[] { 0, 0, 0, 0, 0 });

		renumberContext.writeToIndex = -1;
		
		if (renumberContext.TEST_SCORE==0) {
			renumberContext.switchToNextFile();
			renumberContext.counter = renumberContext.selectDistinct(true, nullvector);
			for (int hl = 0; hl < 5; hl++) {
				int len = Rule.HLH[hl].length;
				for (int hli = 1; hli < len; hli++) {
					renumberContext.switchToNextFile();
					int[] dec = new int[] { 0, 0, 0, 0, 0 };
					dec[hl] = hli;
					HLVector vector = new HLVector(dec);
					doit(vector, renumberContext);
				}
			}

		} else {
			renumberContext.switchToNextFile();
			HLVector vector = new HLVector(renumberContext.TEST_SCORE);
			doit(vector, renumberContext);
		}


		Printer.println("Found " + renumberContext.counter + " digital values.");
		Dictionary dict = new Dictionary();
		dict.setPath(renumberContext.dictFile);
		String v1 = dict.getStringValueFor(32);
		Printer.println("32=" + v1);

		try {

			String fileToRename = renumberContext.getFinalFile();
			String fileToDelete = renumberContext.getUnusedTempFile();
			;

			File f = new File(fileToRename);
			f.renameTo(new File(ctx.getDigitalDataFilePath()));
			f = new File(fileToDelete);
			f.delete();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}
